// pages/user/user.js
import { getUserLikeVideoList, getUserCommentList, deleteComment, getSign, publishSign, VideoLike } from "../../utils/api.js";
import { retTimeInterval } from "../../utils/util.js"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {},
    userTrends: [],
    sign: '暂无',
    loadingNum: 4,
    loadingPage: 1
  },
  /**
   * 生命周期函数--监听页面加载
   */
  getData: function (isCloseLoading) {
    getUserCommentList(this.data.loadingNum, this.data.loadingPage, (data) => {
      data.forEach((item) => {
        item.liketime = item.pubtime
      })
      var origin = this.data.userTrends;
      if (origin) {
        data.forEach((item) => {
          origin.push(item);
        });
        this.setData({
          userTrends: origin
        });
      } else {
        this.setData({
          userTrends: data
        });
      }
      // console.log(data)
      getUserLikeVideoList(this.data.loadingNum, this.data.loadingPage, (data) => {
        if (isCloseLoading) {
          // 隐藏导航栏加载框
          wx.hideNavigationBarLoading();
          // 停止下拉动作
          wx.stopPullDownRefresh();
          // 隐藏加载动画
          wx.hideLoading();
        }
        if (data.length == 0) {
          console.log("没了, 兄dei");
          return;
        }
        var userTrends = this.data.userTrends;
        data.forEach((item) => {
          userTrends.push(item);
        })
        // 根据时间排序
        // console.log(userTrends);
        var len = userTrends.length;
        for (let i = 0; i < len - 1; i++) {
          for (let j = 0; j < len - i - 1; j++) {
            if (userTrends[j].liketime < userTrends[j + 1].liketime) {
              var temp = userTrends[j];
              userTrends[j] = userTrends[j + 1];
              userTrends[j + 1] = temp;
            }
          }
        };
        // 计算时间间隔
        userTrends.forEach((item) => {
          item.interval = retTimeInterval(item.liketime);
        });
        // console.log(userTrends);
        this.setData({
          userTrends: userTrends
        });
      })
    });
  },
  onLoad: function (options) {
    wx.showLoading({
      title: '加载中',
    });
    if (wx.getStorageSync('userInfo')) {
      this.setData({
        userInfo: wx.getStorageSync('userInfo')
      })
    };
    this.getData();
    getSign((res) => {
      if (res.content) {
        this.setData({
          sign: res.content
        })
        console.log('已设置' + res.content);
      }
    })
  },
  submitSign: function (e) {
    this.setData({
      sign: e.detail.value
    })
    publishSign(e.detail.value, (res) => {
      if (res.errcode == 0 && res.errmsg == "success") {
        wx.showToast({
          title: '修改成功',
          icon: "success"
        })
      }
    })
  },
  remove: function (e) {
    // console.log(e);
    var Type = e.target.dataset.type;
    var removeid = e.target.dataset.removeid;
    console.log(Type)
    switch (Type) {
      case "0": {
        VideoLike(removeid, false, (res) => {
          if (res.errcode == 0 && res.errmsg == "success") {
            wx.showToast({
              title: '删除成功',
              icon: "success",
              duration: 1500
            })
            this.getData();
          }
        })
        break;
      }
      case "1": {
        deleteComment(removeid, (res) => {
          if (res.errcode == 0 && res.errmsg == "success") {
            wx.showToast({
              title: '删除成功',
              icon: "success",
              duration: 1500
            })
            this.getData();
          }
        })
        break;
      }
      default: {
        break;
      }
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    wx.hideLoading()
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.showNavigationBarLoading();
    this.getData(true)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    wx.showLoading({
      title: '玩命加载中...',
    });
    var temp = this.data.loadingPage;
    temp++;
    this.setData({
      loadingPage: temp
    })
    this.getData(true);
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  }
})